package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussRechargeConfig;
import cn.pegasus.teamwork.pegasus.mapper.BussRechargeConfigMapper;
import cn.pegasus.teamwork.pegasus.service.BussRechargeConfigService;
import cn.pegasus.teamwork.util.OSSClientUtil;


/**
 * 充值配置
 * @author coco
 *
 */
@Controller
@RequestMapping("/recharge")
public class RechargeController extends BaseManagerController<BussRechargeConfig, BussRechargeConfigMapper>{

	private static final Logger logger = LoggerFactory.getLogger(RechargeController.class);
	
	@Autowired
	private BussRechargeConfigService rechargeConfigService;
	/**
	 * 查询服务列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("recharge/recharge-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			map.put("configStatus", 0);
			PageObject<BussRechargeConfig> dicList = rechargeConfigService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询服务列表信息异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 添加新增页面
	 */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Integer id)throws Exception{
		 if(null == type){
			 ModelAndView view = new ModelAndView("recharge/recharge-add");
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("recharge/recharge-edit");
			 BussRechargeConfig news = rechargeConfigService.findInfo(id);
			 view.addObject("info", news);
			 return view;
		 } 
	 }
	 
	 /**
	  * 新增
	  * newsType 1-帮助中心；2-关于我们；3-其他类型,
	  */
	 @ResponseBody
	 @RequestMapping(value = "/addRecharge", method = RequestMethod.POST)
	 public String addRecharge(HttpServletRequest request,
			 @RequestParam(value = "rechargeMoney", required = false) Double rechargeMoney,
			 @RequestParam(value = "giveMoney", required = false) Double giveMoney,
			 @RequestParam(value = "configName", required = false) String configName,
			 @RequestParam(value = "configType", required = false) Integer configType,
			 @RequestParam(value = "configDesc", required = false) String configDesc){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussRechargeConfig info = new BussRechargeConfig();
        	long chargeMoney = (long) (rechargeMoney*100);
        	info.setRechargeMoney(chargeMoney);
        	if(null != giveMoney){
        		long giveM = (long) (giveMoney*100);
            	info.setGiveMoney(giveM);
        	}
        	info.setConfigName(configName);
        	info.setConfigDesc(configDesc);
        	info.setConfigType(configType);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String configImages = util.uploadImg2Oss(file);
            	info.setConfigImages(configImages);
        	}
        	rechargeConfigService.addRecharge(getAdmin().getId(), info);
        } catch (Exception e) {
            logger.error("新增优惠券异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 修改
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateRecharge", method = RequestMethod.POST)
	 public String updateRecharge(HttpServletRequest request,
			 @RequestParam(value = "rechargeMoney", required = false) Double rechargeMoney,
			 @RequestParam(value = "giveMoney", required = false) Double giveMoney,
			 @RequestParam(value = "configName", required = false) String configName,
			 @RequestParam(value = "configDesc", required = false) String configDesc,
			 @RequestParam(value = "configType", required = false) Integer configType,
			 @RequestParam(value = "id", required = false) Integer id){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussRechargeConfig info = new BussRechargeConfig();
        	long chargeMoney = (long) (rechargeMoney*100);
        	info.setRechargeMoney(chargeMoney);
        	if(null != giveMoney){
        		long giveM = (long) (giveMoney*100);
            	info.setGiveMoney(giveM);
        	}
        	info.setConfigName(configName);
        	info.setConfigDesc(configDesc);
        	info.setConfigType(configType);
        	info.setId(id);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String configImages = util.uploadImg2Oss(file);
            	info.setConfigImages(configImages);
        	}
        	rechargeConfigService.updateRecharge(getAdmin().getId(), info);
        } catch (Exception e) {
            logger.error("修改优惠券信息异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public String delete(@RequestParam(value="id",required = false) Integer id){
        try {
        	rechargeConfigService.delete(getAdmin().getId(), id);
        } catch (Exception e) {
            logger.error("删除区域异常", e);
            return "1001";
        }
        return "200";
	 }
}
